<?php /** @noinspection ALL */

//充值记录

namespace app\admin\controller;

use think\facade\Db;

class RechargeLog {

	function getList($page=1,$pageSize=10){
		$input=input();

		$userinfo=Pubilc::check_jwt($input['token']);

		if(!$userinfo)return json(['code' => 0,'msg'=>'请重新登录']);

		$id=$userinfo->userId;

		$type=$userinfo->type;

		$condition = ' and 1=1';

		if (!empty($input['userinfo'])) {
			$input['userinfo']=trim($input['userinfo']);
			$condition .= " and (su.nick_name like '%{$input['userinfo']}%' or su.tel like '%{$input['userinfo']}%' ) ";
		}

		if (!empty($input['time'])) {
			$start=$input['time'][0]/1000;
			$end=$input['time'][1]/1000;
			$condition .= " and rl.add_time >= $start and rl.add_time <=  $end";
		}

		$recharge_logList = Db::query("select rl.*,su.nick_name,su.real_name,su.tel,FROM_UNIXTIME(rl.add_time,'%Y-%m-%d %H:%i:%s')  as add_time from recharge_log as rl join system_user as su on su.id=rl.uid where 1".$condition." order by rl.id desc limit ".(($page-1)*$pageSize).",$pageSize");

		$total_num = Db::query("select count(rl.id) as sum from recharge_log as rl join system_user as su on su.id=rl.uid where 1".$condition." order by rl.id desc ");

		if($type==2){
			$usertype=2;
		}else{
			$usertype=1;
		}

		return json(['code' => 1, 'data' => $recharge_logList,'total_num'=>$total_num[0]['sum'],'usertype'=>$usertype]);
	}

	//修改状态
	function edit_status(){
		$input = input();

		if(empty($input['id'])||$input['status']=='') return json(['code' => 0, 'msg' => '参数错误']);

		$recharge_log = Db::table('recharge_log')->where('id', $input['id'])->find();

		if (empty($recharge_log)) return json(['code' => 0, 'msg' => '数据不存在']);

		$result = Db::name('recharge_log')->where('id', $input['id'])->update(['status'=>$input['status']]);

		if (!$result) return json(['code' => 0, 'msg' => '编辑失败']);

		return json(['code' => 1, 'msg' => '编辑成功']);
	}

	//删除记录
	function del_log(){
		$input = input();

		if(empty($input['id'])) return json(['code' => 0, 'msg' => '参数错误']);

		$recharge_log = Db::table('recharge_log')->where('id', $input['id'])->find();

		if (empty($recharge_log)) return json(['code' => 0, 'msg' => '数据不存在']);

		$result = Db::name('recharge_log')->where('id', $input['id'])->delete();

		if (!$result) return json(['code' => 0, 'msg' => '删除失败']);

		return json(['code' => 1, 'msg' => '删除成功']);
	}

}